import storage from '@/util/storage'
import api from '@/api'

const state = {
    account: storage.local.get('account') || '',
    username: storage.local.get('username') || '',
    avatar: storage.local.get('avatar') || '',
    token: storage.local.get('token') || '',
    failure_time: storage.local.get('failure_time') || '',
    permissions: []
}

const getters = {
    isLogin: state => {
        let retn = false
        if (state.token) {
            // let unix = Date.parse(new Date())
            // if (unix < state.failure_time * 1000) {
            retn = true
            // }
        }
        return retn
    }
}

const actions = {
    login({commit}, data) {
        return new Promise((resolve, reject) => {
            // 通过 mock 进行登录
            api.post('user/login', data).then(res => {
                commit('setUserData', res.data.data)
                resolve()
            }).catch(error => {
                reject(error)
            })
        })
    },
    logout({commit}) {
        commit('removeUserData')
        commit('menu/invalidRoutes', null, {root: true})
        commit('tabbar/clean', null, {root: true})
    },
    editPassword(context, data) {
        return new Promise((resolve, reject) => {
            api.post('member/member/change-password', data).then(() => {
                resolve()
            }).catch(res => {
                reject(res)
            })
        })
    },
    // 获取我的权限
    getPermissions({commit}) {
        return new Promise(resolve => {
            // 通过 mock 获取权限
            api.post('user/purviews', {
                // params: {
                //     account: state.account
                // }
            }).then(res => {
                // todo remove
                // const mockPermission = [
                //     'system.browse',
                //     'permission.browse'
                // ]
                // const permissions = mockPermission.concat(res.data.data)
                const permissions = res.data.data
                commit('setPermissions', permissions)
                resolve(permissions)
            })
        })
    }
}

const mutations = {
    setUserData(state, data) {
        storage.local.set('account', data.account)
        storage.local.set('username', data.username)
        storage.local.set('avatar', data.avatar)
        storage.local.set('token', data.token)
        storage.local.set('failure_time', data.failure_time)
        state.account = data.account
        state.username = data.username
        state.avatar = data.avatar
        state.token = data.token
        state.failure_time = data.failure_time
    },
    removeUserData(state) {
        storage.local.remove('account')
        storage.local.remove('username')
        storage.local.remove('avatar')
        storage.local.remove('token')
        storage.local.remove('failure_time')
        state.account = ''
        state.username = ''
        state.avatar = ''
        state.token = ''
        state.failure_time = ''
    },
    setPermissions(state, permissions) {
        state.permissions = permissions
    }
}

export default {
    namespaced: true,
    state,
    actions,
    getters,
    mutations
}
